package com.cupk.rotaryguard.monitor.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cupk.common.base.config.Contant;
import com.cupk.rotaryguard.monitor.entity.DeviceDiagnosis;
import com.cupk.rotaryguard.monitor.entity.DeviceInfo;
import com.cupk.rotaryguard.monitor.entity.DeviceMonitorRecord;
import com.cupk.rotaryguard.monitor.mapper.DeviceMapper;
import com.cupk.rotaryguard.monitor.mapper.DiagnosisMapper;
import com.cupk.rotaryguard.monitor.mapper.MonitorRecordMapper;
import com.cupk.rotaryguard.monitor.utils.ClassificationModel;
import com.cupk.rotaryguard.monitor.utils.CsvUtil;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.cupk.rotaryguard.monitor.controller.flask.FlaskHttpClient.*;
import static com.cupk.rotaryguard.monitor.utils.CsvUtil.deleteFileContent;

/**
 * @author tjy
 * @date 2023/4/15
 * @apiNote
 */
@Component
public class DeviceDiagnosisJob {
    @Resource
    private DeviceMapper deviceMapper;
    @Resource
    private MonitorRecordMapper monitorRecordMapper;
    @Resource
    private DiagnosisMapper diagnosisMapper;

    /**
     * 定时诊断任务
     */
    @XxlJob("deviceDiagnosis")
    public void deviceDiagnosisJob() throws Exception {
        Long deviceId = Long.valueOf(XxlJobHelper.getJobParam());
        DeviceInfo curDevice = deviceMapper.selectById(deviceId);
        String deviceModel = curDevice.getDeviceModel();
        DeviceDiagnosis diagnosisRecord = new DeviceDiagnosis();

        //插入诊断表数据
        diagnosisRecord.setDeviceId(deviceId);
        diagnosisRecord.setDeviceModel(deviceModel);

        //获取指定设备号前500条数据用作诊断
        QueryWrapper<DeviceMonitorRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id", deviceId)
                .orderByDesc("record_time")
                .last("limit 0,3500");
        List<DeviceMonitorRecord> monitorRecords = monitorRecordMapper.selectList(queryWrapper);
        List<String[]> recordList = new ArrayList<>();
        for (DeviceMonitorRecord curRecord : monitorRecords) {
            String[] record = new String[3];
            record[0] = String.valueOf(curRecord.getDeTime());
            record[1] = String.valueOf(curRecord.getFeTime());
            record[2] = String.valueOf(curRecord.getRpm());
            recordList.add(record);
        }
        CsvUtil.writeCsvFile(recordList, Contant.TEST_FILE_PATH);
        //调用flask接口进行特征提取
        if(!Contant.SUCCESS_FLASK_VALUE.equals(testCSVToTestTW())){
            return;
        }
        if(!Contant.SUCCESS_FLASK_VALUE.equals(testTWtoTestGena())){
            return;
        }
        if(!Contant.SUCCESS_FLASK_VALUE.equals(getAllFeatureCSV())){
            return;
        }
        if(!Contant.SUCCESS_FLASK_VALUE.equals(selectAllFeature())){
            return;
        }
        //将特征提取到的文件转换为String[][]
        String featureFilePath = Contant.FEATURE_SELECT_FILE_PATH;
        String[][] features = CsvUtil.readCSV(featureFilePath, false);
        //开始诊断
        //结果
        String diagnosisRes = ClassificationModel.diagnosis(features);
        //概率分布
        String probDistribution = ClassificationModel.diagnosisPredictProb(features);
        //插入诊断表数据
        diagnosisRecord.setDeviceId(deviceId);
        diagnosisRecord.setDeviceModel(deviceModel);
        diagnosisRecord.setDiagnosisRes(diagnosisRes);
        diagnosisRecord.setProbDistribution(probDistribution);
        //更新诊断表
        diagnosisMapper.insert(diagnosisRecord);

        //清空文件内容
        try {
            File file1 = new File(Contant.TEST_FILE_PATH);
            File file2 = new File(Contant.TEST_TW_FILE_PATH);
            File file3 = new File(Contant.FEATURE_FILE_PATH);
            File file4 = new File(Contant.FEATURE_SELECT_FILE_PATH);
            deleteFileContent(file1);
            deleteFileContent(file2);
            deleteFileContent(file3);
            deleteFileContent(file4);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

    }
}
